import pytest
from fastapi.testclient import TestClient
from tortoise.contrib.test import finalizer, initializer

from main import app
from src.config import config as settings


@pytest.fixture(scope="session")
def client():
    """创建测试客户端"""
    with TestClient(app) as c:
        yield c


@pytest.fixture(scope="session", autouse=True)
def initialize_tests(request):
    """初始化测试数据库"""
    # 使用MySQL测试数据库进行测试
    db_url = "mysql://aiomysql://root:@localhost:3306/fastapi_tree_test"
    initializer(["src.models"], db_url=db_url, app_label="models")
    request.addfinalizer(finalizer)